<template>
  <div class="app-container">
    <el-scrollbar :wrap-class="{ wrapClass: true }" class="form-scrollbar">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>{{ $t('system.basicInfo') }}</span>
        </div>

        <el-form
          ref="form"
          class="form-company"
          :rules="rules"
          :model="info"
          label-width="180px"
        >
          <el-form-item :label="$t('common.companyName')" prop="name">
            <el-input v-model="info.name" @input="checkTrim('name')" />
          </el-form-item>
          <el-form-item :label="$t('common.accountValidity')">
            <el-input v-model="info.validity" disabled />
          </el-form-item>
          <el-form-item :label="$t('common.openNum')">
            <el-input
              :value="
                info.isOpenUser === 1
                  ? $t('common.noLimit')
                  : info.openUserNum + $t('common.nPeople')
              "
              disabled
            />
          </el-form-item>
          <el-form-item :label="$t('common.points')">
            <template v-if="info.isUnlimitedPoints === 1">
              <el-input :value="$t('common.noLimit')" disabled></el-input>
            </template>
            <template v-else>
              <el-input
                :value="info.numberOfPoints + $t('common.nPoints')"
                disabled
              ></el-input>
            </template>
          </el-form-item>
          <el-form-item :label="$t('common.clueUnique')">
            <el-radio-group v-model="info.isRepetitionAllowed">
              <el-radio :label="0">{{ $t('common.close') }}</el-radio>
              <el-radio :label="1">{{ $t('common.open') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('common.autoWatersDay')">
            <el-input-number
              v-model="info.autoWatersDay"
              :max="999999"
              :min="1"
            ></el-input-number>
          </el-form-item>
        </el-form>
      </el-card>
      <span style="padding-bottom: 30px">
        <el-button type="primary" :loading="infoLoading" @click="subInfo">{{
          $t('common.submit')
        }}</el-button>
      </span>
    </el-scrollbar>
  </div>
</template>
<style scoped>
.app-container {
  padding-top: 20px;
  height: calc(100vh - 65px);
}

.form-scrollbar {
  height: calc(100vh - 75px);
}

.form-company {
  min-width: 720px;
}

.el-dialog-div {
  height: 290px;
}

.el-dialog-div /deep/ .el-alert {
  margin-bottom: 20px;
}

.box-card {
  margin-bottom: 20px;
}

.box-card /deep/ .el-card__header {
  padding: 10px 20px;
}

.box-card /deep/ .el-card__body {
  padding: 20px 10px;
}
</style>

<script>
import { postAction } from '@/api/common'
import common from '@/utils/common'

export default {
  name: 'index',
  data() {
    return {
      info: {
        name: '',
        validity: '',
        isOpenUser: 1,
        openUserNum: 0,
        isRepetitionAllowed: 0,
        autoWatersDay: 15,
        isUnlimitedPoints: 1,
        numberOfPoints: 0,
      },
      infoLoading: true,
      rules: {
        // element form表单元素验证
        name: [
          {
            required: true,
            message: this.$t('common.enter', [this.$t('common.companyName')]),
            trigger: 'change',
          },
        ],
      },
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      this.infoLoading = true
      const { data } = await postAction({}, 'UserCompany/GetSysCompanyConfig')
      this.info = data
      this.infoLoading = false
    },
    subInfo() {
      this.infoLoading = true
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.infoLoading = true
          await postAction(this.info, 'UserCompany/SubSysCompanyConfig')

          this.infoLoading = false
          this.$notify({
            title: this.$t('common.success'),
            message: this.$t('common.operationSuccess'),
            type: 'success',
            duration: 2000,
          })
        }
      })
    },
    checkTrim(obj) {
      this.info[obj] = common.checkAllTrim(this.info[obj])
    },
  },
}
</script>
